<!--考试培训-考证管理-->
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>考证管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="考试名称" prop="bkzname">
        <el-input size="small" style="width: 200px;" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="考试等级：">
        <el-select size="small" v-model="formInline.parentcode" placeholder="请选择" @change="search">
          <el-option label="全部" value="ALL"></el-option>
          <el-option label="一级" value="FEIZHI"></el-option>
          <el-option label="二级" value="JIUYI"></el-option>
          <el-option label="三级" value="JINSHU"></el-option>
          <el-option label="四级" value="SULIAO"></el-option>
          <el-option label="五级" value="JIADIAN"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态：">
        <el-select size="small" v-model="formInline.parentcode" placeholder="请选择" @change="search">
          <!-- <el-option label="全部" value=""></el-option> -->
          <el-option label="未发布" value="unpublic"></el-option>
          <el-option label="已发布" value="public"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="">
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
      </el-form-item>
      <br>
      <div class="addBut">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="itemAdd('add')">创建考证</el-button>
      </div>
    </el-form>
    <!--列表-->
    <el-table size="small" :data="itemList" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" sortable prop="bkz_logo_uri[0].url" label="封面" width="200">
        <template #default="scope">
          <el-image
            style="width: 80px; height: 50px"
            :src="scope.row.bkz_logo_uri[0].url"
            :preview-src-list="[scope.row.bkz_logo_uri[0].url]"
            fit="cover"
          >
            <template #error>
              <div class="image-error">加载失败</div>
            </template>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column align="center" sortable prop="bkz_name" label="考试名称" width="300">
      </el-table-column>
      <el-table-column align="center" sortable prop="bkz_levelnames" label="可提报等级" width="250">
      </el-table-column>
      <el-table-column align="center" sortable prop="item_unit" label="培训价格(元)" width="200">
      </el-table-column>
      <el-table-column align="center" sortable prop="bkz_bmenddate" label="报名截止时间" width="200">
      </el-table-column>
      <el-table-column align="center" sortable prop="bkz_ksdate" label="考试时间" width="200">
      </el-table-column>
      <el-table-column align="center" sortable prop="bkz_publishstate" width="200">
        <template v-slot="scope">
          {{ scope.row.bkz_publishstate === 0 ? '未发布' : scope.row.bkz_publishstate === 1 ? '已发布' : scope.row.bkz_publishstate }}
        </template>
      </el-table-column>
      <el-table-column align="center" sortable prop="item_unit" label="已报名学员" width="200">
      </el-table-column>
      <el-table-column label="操作" min-width="300">
        <template v-slot="scope">
          <el-button size="mini" v-if="scope.row.bkz_publishstate === 0" @click="publicFormBut(scope.$index, scope.row)">发布</el-button>
          <el-button size="mini" v-if="scope.row.bkz_publishstate === 1" @click="unPublicFormBut(scope.$index, scope.row)">下架</el-button>
          <el-button size="mini" @click="selectStudents(scope.$index, scope.row)">查看学员</el-button>
          <el-button size="mini" @click="editFormBut(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="delteFormBut(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
  </div>
</template>
<script>
// 导入请求方法
import Pagination from '../../components/Pagination'
import {deletekz, querykzlist, updatekzstate} from "../../api/InstitutionApi";
export default {
  data() {
    return {
      loading: false, //是显示加载
      // 请求分类明细数据参数
      formInline: {
        page: 1,
        limit: 10,
        deptId: '',
        userName: '',
        userMobile: '',
        parentcode: '全部'
      },
      // 考证列表数据
      itemList:[],
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      }
    }
  },
  // 注册组件
  components: {
    Pagination
  },
  /**
   * 数据发生改变
   */
  watch: {},
  /**
   * 创建完毕
   */
  created() {
    // this.getdata(this.formInline.parentcode)
    this.getdata()
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 获取数据方法
    getdata(parameter) {
      this.loading = true
    // 获取考证列表数据
      querykzlist({
        "bkzname":"", //#考证名称 不传查询所有
        "bkzlevel":"", //#考证等级吧 不传查询所有
        "bkzstatus":"", //#状态0未发布，1已经发布，2已经结束 不传查询所有
      }).then(res => {
        console.log('考证列表', res)
        this.itemList = res.data.base_info
        this.loading = false

      })
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage
      this.formInline.limit = parm.pageSize
      this.getdata(this.formInline)
    },
    //搜索事件
    search() {
      // console.log('item-formInline:',this.formInline.parentcode)
      this.getdata(this.formInline.parentcode)

    },
    // 添加新课程
    itemAdd() {
      // 跳转页面的三种方式
      // 方式1：通过 path 跳转
      // this.$router.push({
      //   path: `/enterprises/EnEdit/:index`
      // });
      // 方式2：通过 name 跳转（推荐）
      // this.$router.push({
      //   name: 'enedit',
      //   params: { index: index }
      // });
      // 方式3：带查询参数（显示在URL中）
      this.$router.push({
        path: '/item/itemAddEdit',
        query: ''
      });
    },
    // 发布
    publicFormBut(index, row) {
      updatekzstate({
        "bkzcode": row.bkz_code, //#考证code
        "publishstate":"1"  //发布状态, 0下架，1发布
      }).then(res => {
        // console.log('kzpublic',res)
        if(res.msg === '发布或者下架考证成功') {
          this.$message({
            type: 'success',
            message: '考证成功发布！'
          })
          this.getdata()
        } else {
          this.$message({
            type: 'info',
            message: res.msg
          })
        }


      })

    },
    // 下架
    unPublicFormBut(index, row) {
      updatekzstate({
        "bkzcode": row.bkz_code, //#考证code
        "publishstate":"0"  //发布状态, 0下架，1发布
      }).then(res => {
        // console.log('kzpublic',res)
        if(res.msg === '发布或者下架考证成功') {
          this.$message({
            type: 'success',
            message: '考试成功下架！'
          })
          this.getdata()
        } else {
          this.$message({
            type: 'info',
            message: res.msg
          })
        }


      })

    },

    // 删除考证
    delteFormBut(index,row) {
      console.log('row:',row)
      deletekz({
        "bkzcode": row.bkz_code
      }).then(res =>{
        console.log('删除',res)
        this.getdata()
      })
    },

    editFormBut(index,row) {
      this.$router.push({
        path: '/item/itemAddEdit',
        query: {rowData : row}
      });
    },
    // 查询报考学员
    selectStudents(index,row) {
      this.$router.push({
        path: '/item/itemStudents',
        query: {rowData : row}
      });
    },
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}

.addBut {
  margin-bottom: 10px;
}
.userRole {
  width: 100%;
}
</style>

